<?php
declare (strict_types=1);

namespace app\platform\controller;

use app\BaseController;
use think\exception\HttpResponseException;
use think\facade\View;

class PlatformBase extends BaseController
{
    public $merchantUser = null;

    public function initialize()
    {
        parent::initialize();
        $this->_managerActions();
    }

    public function show(...$args)
    {
        throw new HttpResponseException(show(...$args));
    }

    private function _managerActions()
    {
        if (!file_exists('../install.lock')) {
            return $this->redirect((string)url('install/index/index'));
        }

        // 校验是否有权限访问这个路由
        $merchantUserObj = new \app\platform\business\Merchant();
        $merchant        = $merchantUserObj->getMerchantUserFromSession();
        $menuBusiness    = new \app\common\business\Menu();
        $roleIds         = $merchant['roleIds']??[];
        $actions         = $menuBusiness->getLoginRolPermissionActions($roleIds);
        $requestPath     = '/platform/' . $this->request->pathinfo();
        if (!strstr($requestPath, 'permission') && !strstr($requestPath, 'login') && !in_array($requestPath, $actions['actionPaths'])) {
            // dump(in_array($requestPath,$actions['actionPaths']));
            // dump($actions['actionPaths']);
            // echo $requestPath;exit;
            if ($this->request->isPost()) {
                return $this->show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PERMISSION_DENY_MESSAGE);
            } else {
                return $this->redirect((string)url('permission/msg'));
            }
        }

        View::assign(['actions' => $actions['menuCodes']]);
    }

    public function isLogin(): bool
    {
        $merchantUserObj    = new \app\platform\business\Merchant();
        $this->merchantUser = $merchantUserObj->getMerchantUserFromSession();
        if (empty($this->merchantUser)) {
            return false;
        }

        return true;
    }

    public function redirect(...$args)
    {
        throw new HttpResponseException(redirect(...$args));
    }
}
